namespace SimpleWebServices.Web.UI.Html.Elements
{
    using System;
    using System.Xml;
    using System.Xml.Serialization;

    [XmlRoot("input")]
    public class FileInput : Input, IXmlSerializable
    {
        public FileInput()
        {
        }

        public FileInput(Name id)
            : base(id)
        {
        }

        public string Accept
        {
            get;
            set;
        }

        public override string Type
        {
            get
            {
                return "file";
            }
        }

        public override IElement Deserialize(string name, string type, string xml)
        {
            IElement result = null;

            if ("input".Equals(name, StringComparison.OrdinalIgnoreCase) && this.Type.Equals(type, StringComparison.OrdinalIgnoreCase))
            {
                result = xml.Deserialize<FileInput>();
            }

            return result;
        }

        public override void ReadXml(XmlReader reader)
        {
            base.ReadXml(reader);

            if (XmlNodeType.Element == reader.NodeType)
            {
                string value = reader.GetAttribute("accept");
                if (!string.IsNullOrEmpty(value))
                {
                    this.Accept = value;
                }
            }
        }

        public override void WriteXml(XmlWriter writer)
        {
            base.WriteXml(writer);

            if (!string.IsNullOrEmpty(this.Accept))
            {
                writer.WriteAttributeString("accept", this.Accept);
            }
        }
    }
}